##################################################################
# CHECK VIVADO VERSION
##################################################################

set scripts_vivado_version 2018.3
set current_vivado_version [version -short]

if { [string first $scripts_vivado_version $current_vivado_version] == -1 } {
  catch {common::send_msg_id "IPS_TCL-100" "ERROR" "This script was generated using Vivado <$scripts_vivado_version> and is being run in <$current_vivado_version> of Vivado. Please run the script in Vivado <$scripts_vivado_version> then open the design in Vivado <$current_vivado_version>. Upgrade the design by running \"Tools => Report => Report IP Status...\", then run write_ip_tcl to create an updated script."}
  return 1
}

##################################################################
# START
##################################################################

# To test this script, run the following commands from Vivado Tcl console:
# source ip.tcl
# If there is no project opened, this script will create a
# project, but make sure you do not have an existing project
# <./Work_Dir/Work_Dir.xpr> in the current working folder.

set list_projs [get_projects -quiet]
if { $list_projs eq "" } {
  create_project Work_Dir Work_Dir -part xcku040-ffva1156-2-i
  set_property target_language Verilog [current_project]
  set_property simulator_language Mixed [current_project]
}

##################################################################
# CHECK IPs
##################################################################

set bCheckIPs 1
set bCheckIPsPassed 1
if { $bCheckIPs == 1 } {
  set list_check_ips { xilinx.com:ip:clk_wiz:6.0 xilinx.com:ip:fifo_generator:13.2 }
  set list_ips_missing ""
  common::send_msg_id "IPS_TCL-1001" "INFO" "Checking if the following IPs exist in the project's IP catalog: $list_check_ips ."

  foreach ip_vlnv $list_check_ips {
  set ip_obj [get_ipdefs -all $ip_vlnv]
  if { $ip_obj eq "" } {
    lappend list_ips_missing $ip_vlnv
    }
  }

  if { $list_ips_missing ne "" } {
    catch {common::send_msg_id "IPS_TCL-105" "ERROR" "The following IPs are not found in the IP Catalog:\n  $list_ips_missing\n\nResolution: Please add the repository containing the IP(s) to the project." }
    set bCheckIPsPassed 0
  }
}

if { $bCheckIPsPassed != 1 } {
  common::send_msg_id "IPS_TCL-102" "WARNING" "Will not continue with creation of design due to the error(s) above."
  return 1
}

##################################################################
# CREATE IP clk_wiz_0
##################################################################

set clk_wiz clk_wiz_0
create_ip -name clk_wiz -vendor xilinx.com -library ip -version 6.0 -module_name $clk_wiz

set_property -dict { 
  CONFIG.PRIM_IN_FREQ {50}
  CONFIG.CLKIN1_JITTER_PS {200.0}
  CONFIG.USE_RESET {false}
  CONFIG.MMCM_DIVCLK_DIVIDE {1}
  CONFIG.MMCM_CLKFBOUT_MULT_F {20.000}
  CONFIG.MMCM_CLKIN1_PERIOD {20.000}
  CONFIG.MMCM_CLKIN2_PERIOD {10.0}
  CONFIG.CLKOUT1_JITTER {162.035}
  CONFIG.CLKOUT1_PHASE_ERROR {164.985}
} [get_ips $clk_wiz]

##################################################################

##################################################################
# CREATE IP fifo_generator_0
##################################################################

set fifo_generator fifo_generator_0
create_ip -name fifo_generator -vendor xilinx.com -library ip -version 13.2 -module_name $fifo_generator

set_property -dict { 
  CONFIG.Fifo_Implementation {Common_Clock_Block_RAM}
  CONFIG.Performance_Options {First_Word_Fall_Through}
  CONFIG.Input_Data_Width {8}
  CONFIG.Input_Depth {1024}
  CONFIG.Output_Data_Width {8}
  CONFIG.Output_Depth {1024}
  CONFIG.Use_Embedded_Registers {true}
  CONFIG.Reset_Pin {false}
  CONFIG.Reset_Type {Asynchronous_Reset}
  CONFIG.Use_Dout_Reset {false}
  CONFIG.Use_Extra_Logic {true}
  CONFIG.Data_Count_Width {11}
  CONFIG.Write_Data_Count_Width {11}
  CONFIG.Read_Data_Count_Width {11}
  CONFIG.Full_Threshold_Assert_Value {1023}
  CONFIG.Full_Threshold_Negate_Value {1022}
  CONFIG.Empty_Threshold_Assert_Value {4}
  CONFIG.Empty_Threshold_Negate_Value {5}
} [get_ips $fifo_generator]

##################################################################

